#include<stdio.h>
#include<string.h>
#include<malloc.h>

#include "sequence_list.h"
/*顺序表*/
/*示例一个图书管理系统*/

#define MAX_LIST_SIZE 1000

/*用于比较两个ElemType变量是否相同*/
static Status_t Compare_Elem_Info(const ElemType *dest,const ElemType *source)
{
    return (dest->price == source->price && strcmp(dest->name,source->name) == 0 && strcmp(dest->token,source->token) == 0);
}
/*用于打印ElemType内容*/
static void Print_Elem(const ElemType *elem)
{
    printf(" %s | %s | %0.1f  |\n",elem->token,elem->name,elem->price);
}
/*用于销毁ElemType*/
static void Destroy_Elem(ElemType *elem)
{
    if (elem)
    {
        if (elem->name)
        {
            free(elem->name);
        }
        if (elem->token)
        {
            free(elem->token);
        }
        free(elem);
    } 
}

/*初始化顺序表*/
Status_t Init_List(S_list_t **list)
{
    *list = (S_list_t *)malloc(sizeof(S_list_t));  
    if (!*list)  
    {  
        return ERROR;  
    }  
    (*list)->elem = (ElemType *)malloc(sizeof(ElemType) * MAX_LIST_SIZE);  
    if (!(*list)->elem)  
    {  
        free(*list);  
        return ERROR;  
    }  
    (*list)->length = 0;  
    (*list)->capacity = MAX_LIST_SIZE;
    return OK;
}

/*获取顺序表长度*/
int get_List_Length(S_list_t *list)
{
    return list->length;
}

/*打印顺序表*/
void Print_List(S_list_t *list)
{
    if (list ==NULL)
    {
        return;
    }
    printf("S_list length = %d\n",list->length);
    printf("S_list capacity = %d\n",list->capacity);

    printf("\n| order |    token   |       name     | price |\n");
    for (int i = 0; i < list->length; i++)
    {
        printf("|   %d   |",(i+1));
        Print_Elem(&(list->elem[i]));
    }
    
}

/*销毁顺序表*/
Status_t Destroy_List(S_list_t **s_list)
{
    S_list_t *list = *s_list;
    if (!list)
    {
        return OK;
    }
    if (list)
    {
        for (size_t i = 0; i < list->length; i++)
        {
            Destroy_Elem(&(list->elem[i]));
        }
        free(list->elem); /*释放元素数组的内存*/  
        free(list);
    }
    *s_list = NULL;  
}  

/*检查顺序表是否需要扩容*/
Status_t is_Need_Capacity(S_list_t *list)
{
    return (list->length == list->capacity);
}

/*扩容顺序表*/
Status_t Increase_Capacity(S_list_t *list)
{
    ElemType *elem = (ElemType *)realloc(list->elem,list->capacity * 2 * sizeof(ElemType));
    if (elem == NULL)
    {
        return ERROR;
    }
    
    list->elem = elem;
    list->capacity *= 2;
    return OK;
}

/*尾部插入元素*/
S_list_t* Push_Back(S_list_t *list,ElemType *elem)
{
    if (list == NULL)
    {
       // return Init_List(list);
       printf("list == NULL\n");
       return NULL;
    }

    list->elem[list->length++] = *elem;/*插入*/

    if (is_Need_Capacity(list))/*判断是否需要扩容*/
    {
        Increase_Capacity(list);
    }
    
    return list;
}

/*尾部删除元素*/
S_list_t* Pop_Back(S_list_t *list)
{
    if (list->length < 1)/*没有元素可以删除*/
    {
        return NULL;
    }
    
    list->length --;

    return list;
}

/*头部插入元素*/
S_list_t* Push_Front(S_list_t *list,ElemType *elem)
{
    if (list == NULL)
    {
        // return Init_List(list);
        return NULL;
    }

    list->length ++;

    for (size_t i = list->length; i >= 2; i--)
    {
        list->elem[i - 1] = list->elem[i - 2];/*往后偏移一位*/
    }
    
    list->elem[0] = *elem;/*插入*/

    if (is_Need_Capacity(list))/*判断是否需要扩容*/
    {
        Increase_Capacity(list);
    }
    
    return list;
}

/*头部删除元素*/
S_list_t* Pop_Front(S_list_t *list)
{
    if (list->length < 1)/*没有元素可以删除*/
    {
        return NULL;
    }

    for (size_t i = 0; i < (list->length - 1); i++)
    {
        list->elem[i] = list->elem[i + 1];
    }
    list->length --;
    
}

/*任意位置插入元素*/
S_list_t* Insert_Elem(S_list_t *list,int order,ElemType *elem)
{
    if (list == NULL)
    {
        // return Init_List(list);
        return NULL;
    }

    if (order < 1 || order > list->length)/*检查序号是否合法*/
    {
        return list;
    }
    
    list->length ++;
    for (size_t i = order - 1; i < list->length; i++)/*插入序号之后的数据向后偏移一位*/
    {
        list->elem[i + 1] = list->elem[i];
    }
    list->elem[order - 1] = *elem;

    if (is_Need_Capacity(list))/*判断是否需要扩容*/
    {
        Increase_Capacity(list);
    }

    return list;
}

/*任意位置删除元素*/
S_list_t* Delete_Elem(S_list_t *list,int order)
{
    if (list->length < 1)/*没有元素可以删除*/
    {
        return NULL;
    }

    if (order < 1 || order > list->length)/*检查序号是否合法*/
    {
        return list;
    }

    for (size_t i = order; i < list->length - 1; i++)
    {
        list->elem[i] = list->elem[i + 1];
    }
    
    list->length --;
}

/*查询数据表：根据元素顺序查询元素*/
ElemType* Find_Elem_use_Order(S_list_t *list,int order)
{
    if (order < 1 || order > list->length)/*查询不合法*/
    {
        return NULL;
    }

    return &(list->elem[order-1]);/*返回对应位置的元素指针*/
}

/*查询数据表：根据元素查询元素顺序*/
int Find_Order_use_Elem(S_list_t *list,ElemType *elem)
{
    if (!elem|| !list)/*查询不合法*/
    {
        return FAIL;
    }

    for (size_t i = 0; i < list->length; i++)
    {
        if(Compare_Elem_Info(&(list->elem[i]),elem))
        {
            return i + 1;/*返回序号*/
        }
    }
    
    return FAIL;
}

/*修改任意位置的元素*/
S_list_t* Change_Elem(S_list_t *list,int order,ElemType *elem)
{
    if (list == NULL)
    {
        return NULL;
    }

    if (order < 1 || order > list->length)/*检查序号是否合法*/
    {
        return list;
    }
    
    list->elem[order - 1] = *elem;

    return list;
}   

